package com.a1573595.musicplayer.show_Lyrics

import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.a1573595.musicplayer.R
import com.a1573595.musicplayer.databinding.ItemLyricsBinding

class LyricsAdapter(private val lyrics: List<Pair<Int, String>>, private var currentLyricPosition: Int) :
    RecyclerView.Adapter<LyricsAdapter.LyricsViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LyricsViewHolder {
        val binding = ItemLyricsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return LyricsViewHolder(binding)
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onBindViewHolder(holder: LyricsViewHolder, position: Int) {
        holder.bind(lyrics[position], position == currentLyricPosition)
    }

    override fun getItemCount(): Int = lyrics.size

    fun setCurrentLyricPosition(position: Int) {
        currentLyricPosition = position
        notifyDataSetChanged()
    }

    class LyricsViewHolder(private val binding: ItemLyricsBinding) : RecyclerView.ViewHolder(binding.root) {
        @RequiresApi(Build.VERSION_CODES.M)
        fun bind(lyric: Pair<Int, String>, isCurrent: Boolean) {
            binding.textViewLyrics.text = lyric.second
            binding.textViewLyrics.setTextColor(
                if (isCurrent) binding.root.context.getColor(android.R.color.holo_red_dark)
                else binding.root.context.getColor(android.R.color.black)
            )
        }
    }
}
